<?php
require_once 'Connection.php';

/**
 * class Rss
 *
 * id, name, url
 *
 */
class Rss {
	
	/**
	 * Create
	 *
	 * @return bool 
	 * Auto set Id field
	 */
	public static function Create(&$obj) {
		$con = Connection::GetConnection();
		$query = sprintf("INSERT INTO rss(name, url) VALUES (N'%s', '%s')",
					mysql_real_escape_string($obj['name']),
					mysql_real_escape_string($obj['url'])
				);
		$result = mysql_query($query, $con);
		$obj['id'] = mysql_insert_id($con);
		return $result;
	}
	
	/**
	 * Update
	 *
	 * @return bool 
	 */
	public static function Update($obj) {
		$con = Connection::GetConnection();
		$query = sprintf("UPDATE rss SET name = N'%s', url = '%s' WHERE id = %d",
					mysql_real_escape_string($obj['name']),
					mysql_real_escape_string($obj['url']),
					$obj['id']
				);
		$result = mysql_query($query, $con);
		return $result;
	}
	
	/**
	 * Read
	 *
	 * @return rss obj
	 */
	public static function Read($Id) {
		$con = Connection::GetConnection();
		$query = sprintf("SELECT name, url FROM rss WHERE id = %d", $Id);
		$lst = mysql_query($query, $con);
		if (mysql_num_rows($lst) != 1) return null;
		$item = mysql_fetch_assoc($lst);
		return $item;
	}
	
	/**
	 * Delete
	 *
	 * @return bool
	 */
	public static function Delete($obj) {
		$con = Connection::GetConnection();
		$query = sprintf("DELETE FROM rss WHERE id = %d", $obj['id']);
		$result = mysql_query($query, $con);
		return $result;
	}
	
	/**
	 * Select rss by keyword
	 *
	 * @return array of rss obj
	 */
	public static function Select($keyWord) {
		$con = Connection::GetConnection();

		$query = "SELECT id, name, url FROM rss";
		if ($keyWord != "")	$query .= sprintf(" WHERE name LIKE '%%%s%%'", $keyWord);
			
		$lst = mysql_query($query, $con);
		$result = array();
		while ($item = mysql_fetch_assoc($lst))
			array_push($result, $item);
		return $result;
	}
	
}
?>